#ifndef DIRSCANNER_H
#define DIRSCANNER_H

// 考虑跨平台可选择<filesystem>//c++17

#include <vector>
#include <string>
#include <dirent.h>
#include <unistd.h>

#include "Configuration.h"

using std::vector;
using std::string;

class DirScanner
{
public:
    DirScanner()
    {}

    void operator()()
    {
        Configuration& config = Configuration::getInstance(""); //这里是不是随便写个空就行
        map<string, string> configMap = config.getConfigMap();

        // 需要加入错误处理
        string key_dir = "dirname";
        string dirname = configMap[key_dir];

        traverse(dirname);
    }

    vector<string>& getfiles() {
        return _files;
    }

private:
    // 根据dirname遍历此文件下所有文件的绝对路径，存放到_files中
    void traverse(const string &dirname) {
        DIR* dir = opendir(dirname.c_str());
        // 错误处理
        if (dir == nullptr) {
            std::cerr << "Failed to open directory: " << dirname << std::endl;
            return;
        }

        dirent* entry;
        while ((entry = readdir(dir)) != nullptr) {
        std::string fileName = entry->d_name;

        // 忽略当前目录和上级目录
        if (fileName == "." || fileName == "..") {
            continue;
        }

        std::string filePath = dirname + "/" + fileName;
        if (entry->d_type == DT_DIR) {
            // 递归遍历子目录
            traverse(filePath);
        } else if (entry->d_type == DT_REG) {
            // 将文件路径存储到vector中
            _files.push_back(filePath);
        }
    }

    closedir(dir);

    }

private:
    vector<string> _files;
};

#endif
